package com.study.web.controller;

import com.study.common.result.R;
import com.study.rds.model.User;
import com.study.module.ratelimit.RateLimit;
import com.study.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RateLimit(name = "getUser", count = 100, period = 1, timeUnit = TimeUnit.MINUTES)
    @GetMapping("/getById")
    public R<User> getById(@RequestParam int id) {
        return Optional.ofNullable(userService.getUserById(id)).map(R::ok).orElseGet(R::fail);
    }
}
